package com.xyz.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.xyz.entity.R;
import com.xyz.vod.service.VodService;
import com.xyz.utils.AliyunVodSDKUtils;
import com.xyz.utils.VodPropertiesUtil;


import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author zsr
 * @date 2021/6/16 9:18
 */
@Schema(description="阿里云视频点播微服务")
//@CrossOrigin //跨域
@RestController
@RequestMapping("/eduvod/video")
public class VodController {

    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @Schema(description="上传视频阿里云点播")
    @PostMapping("uploadAliyunVideo")
    public R uploadAliyunVideo(MultipartFile file){
//        String videoId= vodService.uploadAly(file);

//        String videoId= vodService.uploadLocalVideo(file);
        String videoId= vodService.uploadLocalVideoM3u8(file);
        return R.ok().data("videoId",videoId);
    }

//    @Schema(description="上传视频到本地")
//    @PostMapping("uploadLocalVideo")
//    public R uploadLocalVideo(@Schema(name = "file", description = "视频文件", required = true)
//                            MultipartFile file) {
//        String videoUrl= vodService.uploadLocalVideo(file);
//        return R.ok().data("videoUrl",videoUrl);
//    }


    @Schema(description="删除单个视频")
    @DeleteMapping("{videoId}")
    public R removeVideo(@Schema(name = "videoId", description = "云端视频id", required = true)
                         @PathVariable String videoId){

        vodService.removeVideo(videoId);
        return R.ok().message("视频删除成功");
    }

    /**
     * 批量删除视频
     * @param videoIdList
     * @return
     */
    @Schema(description="批量删除视频")
    @DeleteMapping("delete-batch")
    public R removeVideoList(
            @Schema(name = "videoIdList", description = "云端视频id", required = true)
            @RequestParam("videoIdList") List<String> videoIdList){

        vodService.removeVideoList(videoIdList);
        return R.ok().message("视频删除成功");
    }

    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id)throws Exception{
//获取阿里云存储相关常量
        String accessKeyId = VodPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = VodPropertiesUtil.ACCESS_KEY_SECRET;

        //初始化
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret);

        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(id);

        //响应

        GetVideoPlayAuthResponse response = client.getAcsResponse(request);

        //得到播放凭证
        String playAuth = response.getPlayAuth();

        //返回结果
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);
    }
}
